/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utilidades;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 *
 * @author humberto
 */
public class imagenUtils {
    
    public static boolean esImagenAdecuada( ImageIcon tmpIconAux){
    
        
        if(tmpIconAux.getIconHeight() > 190 || tmpIconAux.getIconWidth() > 250)
            return false;    
    return true;
    }
    
    
     public static ImageIcon escalaImagenGrande(ImageIcon tmpIconAux)    {

        
        int anchoMax=250;
        int altoMax=190;
        
        int ancho = tmpIconAux.getIconWidth();
        int alto = tmpIconAux.getIconHeight();
       
        if (ancho < alto) {alto = (alto * anchoMax) / ancho;ancho = anchoMax;}      
        else {ancho = (ancho * altoMax) / alto;alto = altoMax;}
     
        ImageIcon tmpIcon = new ImageIcon(tmpIconAux.getImage().getScaledInstance(ancho, alto, Image.SCALE_DEFAULT));
        return tmpIcon;

    }
     
     
     public static  boolean existeImagen(String direcion,String nombre){
         
         if(new File(direcion+nombre+".png").exists())   
             return true;
                
         return false;
     }
     
     public static boolean tieneCanalAlfa(Image image) {
         
            if (image instanceof BufferedImage) {
                BufferedImage bimage = (BufferedImage)image;
                return bimage.getColorModel().hasAlpha();
            }

            PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
            
            try {
                pg.grabPixels();
            } catch (InterruptedException e) {
            }

            ColorModel cm = pg.getColorModel();
            
            return cm.hasAlpha();
}
     
     
     public static BufferedImage imageToBFImage(Image image) {
         
            if (image instanceof BufferedImage) {
                return (BufferedImage)image;
            }

            image = new ImageIcon(image).getImage();

            boolean hasAlpha = tieneCanalAlfa(image);

            BufferedImage bimage = null;
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        
            try {
                int transparency = Transparency.OPAQUE;

                        if (hasAlpha) {
                            transparency = Transparency.BITMASK;
                        }

                    GraphicsDevice gs = ge.getDefaultScreenDevice();
                    GraphicsConfiguration gc = gs.getDefaultConfiguration();
                    bimage = gc.createCompatibleImage(
                    image.getWidth(null), image.getHeight(null), transparency);

                } catch (HeadlessException e){}
            
            

            if (bimage == null) {
             
                int type = BufferedImage.TYPE_INT_RGB;
                    if (hasAlpha) {
                        type = BufferedImage.TYPE_INT_ARGB;
                     }
            bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
            }
 
            
           Graphics g = bimage.createGraphics();

           g.drawImage(image, 0, 0, null);
           g.dispose();

    return bimage;
}

     public static void guardarFoto(String direccion, ImageIcon figura, String nombre){
        
         BufferedImage imagen = imageToBFImage(figura.getImage());
         
         try {
            ImageIO.write(imagen, "png", 
             new File(direccion+nombre+".png"));
            } catch (IOException e) {
            System.out.println("Error de escritura");
            }
        
        
     }
     
     public static boolean eliminarFoto(String direccion, String nombre){
        
     if(new File(direccion+nombre+".png").delete()) 
        return true;
         return false;
        
     }
    
}
